<?php

namespace App\Http\Controllers\Shop\Business;

use App\Http\Controllers\ShopController;
use App\Models\Ems as EmsModel;
use App\Models\Business\Business as BusinessModel;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Validator;

class EmailController extends ShopController
{
    public function send()
    {
        // return 111;
        
        $business = request()->get('business');

        $email = request('email',$business['email']);

        if(empty($email))
        {
            return $this->error('邮箱不能为空');
        }

        // 条件数组
        $where = [
            'event' => 'email',
            'email' => $email,
            'times' => 0
        ];

        $Ems = EmsModel::where($where)->first();
        // return $Ems;
        if($Ems)
        {
            return $this->error('已发送验证码，请查看邮箱');
        }

        DB::beginTransaction();

        // 生成验证码
        $code = build_randstr(4);
        
        $EmsData = [
            'event' => 'email',
            'email' => $email,
            'times' => 0,
            'code' => $code,
            'ip' => request()->ip()
        ];

        $validate = [
            [
                'event'  => 'required',
                'email'   => 'required',
                'code' => 'required',
                'ip' => 'required',
            ],
            [
                'event.required' => '事件必填',
                'email.required'     => '邮箱必须填写',
                'code.required'   => '验证码未知', 
                'ip.required'   => 'ip地址未知', 
            ]
        ];

        $validator = Validator::make($EmsData,...$validate);

        if($validator->fails())
        {
            return $this->error($validator->errors()->first());
        }

        $EmsStatus = EmsModel::create($EmsData);
        
        $SendStatus = Mail::send('shop.business.email',['code' => $code],function($message) use ($email) {
            $message->to($email)->subject('云平台');
        });

        if($EmsStatus === false || $SendStatus === false)
        {
            DB::rollBack();
            return $this->error('邮箱验证码发送收失败');
        }else{
            DB::commit();
            return $this->success('邮箱验证码发送成功');
        }
    }

    public function check()
    {
        $business = request()->get('business');
        $code = request('code','');
        $email = request('email',$business['email']);

        if(empty(trim($code)))
        {
            return $this->error('验证码不能为空');
        }

        if(empty($email))
        {
            return $this->error('邮箱不能为空');
        }

        $where = [
            'event' => 'email',
            'email' => $email,
            'code' => trim($code)
        ];

        $Ems = EmsModel::where($where)->first();

        if(!$Ems)
        {
            return $this->error('验证码错误');
        }

        // 开启事务
        DB::beginTransaction();

        // 更新用户认证字段
        $business->auth = '1';

        $BusinessStatus = $business->save();

        $EmsStatus = $Ems->delete();

        if($BusinessStatus === false || $EmsStatus === false)
        {
            DB::rollBack();
            return $this->error('认证失败');
        }else{
            DB::commit();
            return $this->success('认证成功');
        }
    }
}
